package com.lancoo.answer.ui.answerSheet.vh;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.entity.AnswerSheetBean;
import com.lancoo.answer.model.entity.AnswerSheetChildBean;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.util.paperUtils.KindIndexStringUtils;
import com.lancoo.answer.widget.ChildFlowLayout;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by pengmi on 2022/2/22 9:06
 * Description:练习场景VH
 */
public class PracticeVH extends AnswerSheetBaseVH{



    public static PracticeVH getPracticeVH(@NonNull ViewGroup parent){
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.ev_item_type_answer_sheet, parent, false);
        return new PracticeVH(inflate);
    }


    public PracticeVH(@NonNull View itemView) {
        super(itemView);
    }


    public void initData(int position,final AnswerSheetBean answerSheetBean){
        TextView tv_kindName=itemView.findViewById(R.id.tv_kind_name);
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean!=null&&taskControlBean.isHideAnswerSheetTypeName()){
            tv_kindName.setVisibility(View.GONE);
        }else{
            tv_kindName.setVisibility(View.VISIBLE);
        }
        int typeIndex = answerSheetBean.getTypeIndex();
        if(typeIndex!=0){
            typeIndex=typeIndex-1;
        }else{
            typeIndex=position;
        }
        String name= KindIndexStringUtils.getOrderIndex(typeIndex)+"、"+answerSheetBean.getKindTypeName();
        tv_kindName.setText(name);
        ChildFlowLayout childFlowLayout = itemView.findViewById(R.id.child_flow_layout);
        childFlowLayout.removeAllViews();
        for (AnswerSheetChildBean answerSheetChildBean:answerSheetBean.getAnswerSheetChildBeanList()){
            childFlowLayout.addView(addView(answerSheetChildBean));
        }
        childFlowLayout.setOnItemClickListener((position1, view) -> {
            if(System.currentTimeMillis()- ConstantBean.Companion.getOnClickSubTime()<200){
                return;
            }
            ConstantBean.Companion.setOnClickSubTime(System.currentTimeMillis());
            AnswerSheetChildBean bean = answerSheetBean.getAnswerSheetChildBeanList().get(position1);
            onClickAnswerSheet(bean);
        });
    }

    public View addView(AnswerSheetChildBean bean){
        LayoutInflater layoutInflater = LayoutInflater.from(itemView.getContext());
        @SuppressLint("InflateParams") View inflate = layoutInflater .inflate(R.layout.ev_item_child_answer,null);
        TextView textView=inflate.findViewById(R.id.tv_child_index);
        textView.setText(bean.getSortIndex());
        ImageView imageView=inflate.findViewById(R.id.img_select_flag);
        imageView.setVisibility(bean.getIsSelected()==0?View.INVISIBLE:View.VISIBLE);
        inflate.findViewById(R.id.img_mark).setVisibility(bean.isMark()?View.VISIBLE:View.GONE);
        textView.setTextColor(0xffffffff);
        switch (bean.getShowState()){
            case 1:
            case 4:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_blue);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_09f);
                break;
            case 2:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_red);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_red);
                break;
            case 3:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_green);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_green);
                break;
            default:
                textView.setTextColor(0xff666666);
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_white);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
        }
        return inflate;
    }
}
